﻿using Microsoft.AspNetCore.Mvc;
using YDT_SeckillService.Applictions.DTOs;
using YDT_SeckillService.Applictions.Interfaces;

namespace YDT_SeckillService.Api.Controllers
{
    /// <summary>
    /// 秒杀活动控制器 - 处理秒杀活动相关的HTTP请求
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class SeckillsController : ControllerBase
    {
        private readonly ISeckillService _seckillService;

        /// <summary>
        /// 构造函数 - 依赖注入秒杀服务
        /// </summary>
        public SeckillsController(ISeckillService seckillService)
        {
            _seckillService = seckillService;
        }

        /// <summary>
        /// 获取所有秒杀活动
        /// </summary>
        /// <returns>秒杀活动列表</returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<SeckillDto>>> GetSeckills()
        {
            var seckills = await _seckillService.GetAllSeckillsAsync();
            return Ok(seckills);
        }

        /// <summary>
        /// 根据ID获取秒杀活动
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <returns>秒杀活动信息</returns>
        [HttpGet("{id:guid}")]
        public async Task<ActionResult<SeckillDto>> GetSeckill(Guid id)
        {
            var seckill = await _seckillService.GetSeckillByIdAsync(id);
            if (seckill == null)
            {
                return NotFound($"秒杀活动ID '{id}' 不存在");
            }
            return Ok(seckill);
        }

        /// <summary>
        /// 根据日期获取秒杀活动
        /// </summary>
        /// <param name="date">日期 (格式: yyyy-MM-dd)</param>
        /// <returns>秒杀活动列表</returns>
        [HttpGet("date/{date}")]
        public async Task<ActionResult<IEnumerable<SeckillDto>>> GetSeckillsByDate(string date)
        {
            if (!DateOnly.TryParse(date, out var dateOnly))
            {
                return BadRequest("日期格式不正确，请使用 yyyy-MM-dd 格式");
            }

            var seckills = await _seckillService.GetSeckillsByDateAsync(dateOnly);
            return Ok(seckills);
        }

        /// <summary>
        /// 获取正在进行中的秒杀活动
        /// </summary>
        /// <returns>秒杀活动列表</returns>
        [HttpGet("active")]
        public async Task<ActionResult<IEnumerable<SeckillDto>>> GetActiveSeckills()
        {
            var seckills = await _seckillService.GetActiveSeckillsAsync();
            return Ok(seckills);
        }

        /// <summary>
        /// 获取即将开始的秒杀活动
        /// </summary>
        /// <returns>秒杀活动列表</returns>
        [HttpGet("upcoming")]
        public async Task<ActionResult<IEnumerable<SeckillDto>>> GetUpcomingSeckills()
        {
            var seckills = await _seckillService.GetUpcomingSeckillsAsync();
            return Ok(seckills);
        }

        /// <summary>
        /// 分页查询秒杀活动
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns>分页秒杀活动列表</returns>
        [HttpGet("paged")]
        public async Task<ActionResult> GetPagedSeckills([FromQuery] SeckillQueryDto query)
        {
            var (seckills, totalCount) = await _seckillService.GetPagedSeckillsAsync(query);
            return Ok(new { Seckills = seckills, TotalCount = totalCount });
        }

        /// <summary>
        /// 创建秒杀活动
        /// </summary>
        /// <param name="createSeckillDto">创建秒杀活动参数</param>
        /// <returns>创建的秒杀活动信息</returns>
        [HttpPost]
        public async Task<ActionResult<SeckillDto>> CreateSeckill(CreateSeckillDto createSeckillDto)
        {
            try
            {
                var seckill = await _seckillService.CreateSeckillAsync(createSeckillDto);
                return CreatedAtAction(nameof(GetSeckill), new { id = seckill.Id }, seckill);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 更新秒杀活动
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <param name="updateSeckillDto">更新秒杀活动参数</param>
        /// <returns>更新后的秒杀活动信息</returns>
        [HttpPut("{id:guid}")]
        public async Task<ActionResult<SeckillDto>> UpdateSeckill(Guid id, UpdateSeckillDto updateSeckillDto)
        {
            var seckill = await _seckillService.UpdateSeckillAsync(id, updateSeckillDto);
            if (seckill == null)
            {
                return NotFound($"秒杀活动ID '{id}' 不存在");
            }
            return Ok(seckill);
        }

        /// <summary>
        /// 删除秒杀活动
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id:guid}")]
        public async Task<ActionResult> DeleteSeckill(Guid id)
        {
            var result = await _seckillService.DeleteSeckillAsync(id);
            if (!result)
            {
                return NotFound($"秒杀活动ID '{id}' 不存在");
            }
            return NoContent();
        }

        /// <summary>
        /// 启用秒杀活动
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <returns>启用后的秒杀活动信息</returns>
        [HttpPatch("{id:guid}/enable")]
        public async Task<ActionResult<SeckillDto>> EnableSeckill(Guid id)
        {
            var seckill = await _seckillService.EnableSeckillAsync(id);
            if (seckill == null)
            {
                return NotFound($"秒杀活动ID '{id}' 不存在");
            }
            return Ok(seckill);
        }

        /// <summary>
        /// 禁用秒杀活动
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <returns>禁用后的秒杀活动信息</returns>
        [HttpPatch("{id:guid}/disable")]
        public async Task<ActionResult<SeckillDto>> DisableSeckill(Guid id)
        {
            var seckill = await _seckillService.DisableSeckillAsync(id);
            if (seckill == null)
            {
                return NotFound($"秒杀活动ID '{id}' 不存在");
            }
            return Ok(seckill);
        }

        /// <summary>
        /// 添加秒杀商品
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <param name="productDto">秒杀商品参数</param>
        /// <returns>更新后的秒杀活动信息</returns>
        [HttpPost("{id:guid}/products")]
        public async Task<ActionResult<SeckillDto>> AddSeckillProduct(Guid id, CreateSeckillProductDto productDto)
        {
            try
            {
                var seckill = await _seckillService.AddSeckillProductAsync(id, productDto);
                if (seckill == null)
                {
                    return NotFound($"秒杀活动ID '{id}' 不存在");
                }
                return Ok(seckill);
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 移除秒杀商品
        /// </summary>
        /// <param name="id">秒杀活动ID</param>
        /// <param name="seckillProductId">秒杀商品ID</param>
        /// <returns>更新后的秒杀活动信息</returns>
        [HttpDelete("{id:guid}/products/{seckillProductId:guid}")]
        public async Task<ActionResult<SeckillDto>> RemoveSeckillProduct(Guid id, Guid seckillProductId)
        {
            var seckill = await _seckillService.RemoveSeckillProductAsync(id, seckillProductId);
            if (seckill == null)
            {
                return NotFound($"秒杀活动ID '{id}' 或秒杀商品ID '{seckillProductId}' 不存在");
            }
            return Ok(seckill);
        }
    }
}
